package com.app;

import java.util.ArrayList;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;

import android.app.Application;
import android.content.Context;
import android.os.Handler;
import cn.jpush.android.api.JPushInterface;
import cn.sharesdk.framework.ShareSDK;

import com.easemob.EMCallBack;
import com.easemob.chatuidemo.DemoHXSDKHelper;
import com.easemob.chatuidemo.domain.User;
import com.google.android.imageloader.ImageLoader;
import com.http.EmptyTask;
import com.matang.R;
import com.modle.PlaceStruct;
import com.util.JsonUtil;

public class DemoApplication extends Application {
	public Handler handler;
	public Handler getHandler() {
		return handler;
	}

	public void setHandler(Handler handler) {
		this.handler = handler;
	}

	public static Context applicationContext;
	private static DemoApplication instance;
	// login user name
	public final String PREF_USERNAME = "username";
	public static boolean isTalk = false;
	/**
	 * 当前用户nickname,为了苹果推送不是userid而是昵称
	 */
	public static String currentUserNick = "测试";
	public static DemoHXSDKHelper hxSDKHelper = new DemoHXSDKHelper();

	public static Context mContext;
	public static ArrayList<PlaceStruct> placeList;// 静态存储地点数据

	ImageLoader mLoader;

	@Override
	public void onCreate() {

		super.onCreate();
		// 运行一个空的Task，解决在2.3系统中，Fragment与AyncTask混合使用时找不到类Bug
		new EmptyTask().execute();
		mContext = getApplicationContext();
		ShareSDK.initSDK(this);
		
		
         super.onCreate();
         
         JPushInterface.setDebugMode(true); 	
         
         // 设置开启日志,发布时请关闭日志
         JPushInterface.init(this);     		// 初始化 JPush
		
		initData();
	      applicationContext = this;
	        instance = this;

	        /**
	         * this function will initialize the HuanXin SDK
	         * 
	         * @return boolean true if caller can continue to call HuanXin related APIs after calling onInit, otherwise false.
	         * 
	         * 环信初始化SDK帮助函数
	         * 返回true如果正确初始化，否则false，如果返回为false，请在后续的调用中不要调用任何和环信相关的代码
	         * 
	         * for example:
	         * 例子：
	         * 
	         * public class DemoHXSDKHelper extends HXSDKHelper
	         * 
	         * HXHelper = new DemoHXSDKHelper();
	         * if(HXHelper.onInit(context)){
	         *     // do HuanXin related work
	         * }
	         */
	        hxSDKHelper.onInit(applicationContext);
	}

	@Override
	public void onTerminate() {
		ShareSDK.stopSDK(this);
		super.onTerminate();
	}

	@Override
	public Object getSystemService(String name) {
		if (name.equals(ImageLoader.IMAGE_LOADER_SERVICE)) {
			if (mLoader == null) {
				mLoader = new ImageLoader(6);
			}
			return mLoader;
		}
		return super.getSystemService(name);
	}

	public void initData() {
		// 异步初始化点点数据
		new Thread(placeRunnable).start();

	}

	Runnable placeRunnable = new Runnable() {

		@Override
		public void run() {
			try {
				placeList = JsonUtil.readPlaceTree(new JSONArray(mContext
						.getString(R.string.new_place_str)));
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
	};
	public static DemoApplication getInstance() {
		return instance;
	}
 
	/**
	 * 获取内存中好友user list
	 *
	 * @return
	 */
	public Map<String, User> getContactList() {
	    return hxSDKHelper.getContactList();
	}

	/**
	 * 设置好友user list到内存中
	 *
	 * @param contactList
	 */
	public void setContactList(Map<String, User> contactList) {
	    hxSDKHelper.setContactList(contactList);
	}

	/**
	 * 获取当前登陆用户名
	 *
	 * @return
	 */
	public String getUserName() {
	    return hxSDKHelper.getHXId();
	}

	/**
	 * 获取密码
	 *
	 * @return
	 */
	public String getPassword() {
		return hxSDKHelper.getPassword();
	}

	/**
	 * 设置用户名
	 *
	 * @param user
	 */
	public void setUserName(String username) {
	    hxSDKHelper.setHXId(username);
	}

	/**
	 * 设置密码 下面的实例代码 只是demo，实际的应用中需要加password 加密后存入 preference 环信sdk
	 * 内部的自动登录需要的密码，已经加密存储了
	 *
	 * @param pwd
	 */
	public void setPassword(String pwd) {
	    hxSDKHelper.setPassword(pwd);
	}

	/**
	 * 退出登录,清空数据
	 */
	public void logout(final EMCallBack emCallBack) {
		// 先调用sdk logout，在清理app中自己的数据
	    hxSDKHelper.logout(emCallBack);
	}
}
